<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h2>类加载器</h2>
    <h3>类的加载</h3>
    <p>当程序要使用某个类时，如果该类还未被加载到内存中，则系统会通过加载，链接，初始化三步来实现对这个类进行初始化。</p>
    <p>加载就是指将class文件读入内存，并为之创建一个Class对象。任何类被使用时系统都会建立一个Class对象。</p>
    <p>链接指的是将Java类的二进制代码合并到JVM的运行状态之中的过程。在链接之前，这个类必须被成功加载。类的链接包括验证、准备和解析等几个步骤。</p>
    <ul>
    <li>验证：是否有正确的内部结构，并和其他类协调一致。</li>
    <li>准备：负责为类的静态成员分配内存，并设置默认初始化值</li>
    <li>解析：将类的二进制数据中的符号引用替换为直接引用</li>
    </ul>
    <p>初始化就是以前讲过的初始化步骤，类初始化时机：</p>
    <ul>
    <li>创建类的实例</li>
    <li>访问类的静态变量，或者为静态变量赋值</li>
    <li>访问类的静态方法</li>
    <li>使用反射方式来强制创建某个类或接口对应的java.lang.Class对象</li>
    <li>初始化某个类的子类</li>
    <li>直接使用java.exe命令来运行某个主类</li>
    </ul>
    <h3>类加载器</h3>
    <p>负责将.class文件加载到内存中，并为之生成对应的Class对象。虽然我们不需要关心类加载机制，但是了解这个机制我们就能更好的理解程序的运行。</p>
    <p>类加载器的组成</p>
    <p>Bootstrap ClassLoader 根类加载器，也被称为引导类加载器，负责Java核心类的加载。比如System,String等。在JDK中JRE的lib目录下rt.jar文件中。</p>
    <p>Extension ClassLoader 扩展类加载器，负责JRE的扩展目录中jar包的加载，在JDK中JRE的lib目录下ext目录。</p>
    <p>System ClassLoader 系统类加载器，负责在JVM启动时加载来自java命令的class文件，以及classpath环境变量所指定的jar包和类路径。</p>
    <p>通过这些描述就可以知道我们常用的类，都是由谁来加载完成的。到目前为止我们已经知道把class文件加载到内存了，那么，如果我们仅仅站在这些class文件的角度，我们如何来使用这些class文件中的内容呢?这就是反射要研究的内容。</p>
    <h2>反射</h2>
    <p>JAVA反射机制是在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。</p>
    <p>要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象。</p>
    <h3>Class类</h3>
    <p>阅读API的Class类得知，Class&nbsp;没有公共构造方法。Class&nbsp;对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass&nbsp;方法自动构造的。</p>
    <p>获取Class对象的三种方式</p>
    <p>方式一: 通过Object类中的getClass()方法</p>
    <div class="cnblogs_code">
    <pre>Person p = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Person();
    Class c </span>= p.getClass();</pre>
    </div>
    <p>方式二:&nbsp;通过 类名.class 获取到字节码文件对象（任意数据类型都具备一个class静态属性,看上去要比第一种方式简单）。</p>
    <div class="cnblogs_code">
    <pre>Class c2 = Person.<span style="color: rgba(0, 0, 255, 1)">class</span>;</pre>
    </div>
    <p>方式三:&nbsp;通过Class类中的方法（将类名作为字符串传递给Class类中的静态方法forName即可）。</p>
    <p>Class c3 = Class.forName("Person");</p>
    <p>前两种你必须明确Person类型。第三种是指定这种类型的字符串就行.这种扩展更强.我不需要知道你的类.我只提供字符串,按照配置文件加载就可以了。</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 获取.class字节码文件对象的方式
    * 1：通过Object类中的getClass()方法
    * 2: 通过 类名.class 获取到字节码文件对象
    * 3: 反射中的方法,
    * public static Class&lt;?&gt; forName(String className) throws ClassNotFoundException
    * 返回与带有给定字符串名的类或接口相关联的 Class 对象
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ReflectDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1：通过Object类中的getClass()方法</span>
            Person p1 = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Person();
            Class c1 </span>=<span style="color: rgba(0, 0, 0, 1)"> p1.getClass();
            System.out.println(</span>"c1 = " +<span style="color: rgba(0, 0, 0, 1)"> c1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2: 通过 类名.class 获取到字节码文件对象</span>
            Class c2 = Person.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">;
            System.out.println(</span>"c2 = " +<span style="color: rgba(0, 0, 0, 1)"> c2);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3: 反射中的方法</span>
            Class c3 = Class.forName("typeinfo.Person");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 包名.类名</span>
            System.out.println("c3 = " +<span style="color: rgba(0, 0, 0, 1)"> c3);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">c1 = class typeinfo.Person
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">c2 = class typeinfo.Person
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">c3 = class typeinfo.Person</span></pre>
    </div>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Person {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成员变量</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String address;
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">构造方法</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person() {
            System.out.println(</span>"空参数构造方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Person(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            System.out.println(</span>"带有String的构造方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有的构造方法</span>
        <span style="color: rgba(0, 0, 255, 1)">private</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
            System.out.println(</span>"带有String，int的构造方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age, String address){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.address =<span style="color: rgba(0, 0, 0, 1)"> address;
            System.out.println(</span>"带有String, int, String的构造方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成员方法
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">没有返回值没有参数的方法</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method1(){
            System.out.println(</span>"没有返回值没有参数的方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">没有返回值，有参数的方法</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method2(String name){
            System.out.println(</span>"没有返回值，有参数的方法 name= "+<span style="color: rgba(0, 0, 0, 1)"> name);
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有返回值，没有参数</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> method3(){
            System.out.println(</span>"有返回值，没有参数的方法"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> 123<span style="color: rgba(0, 0, 0, 1)">;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有返回值，有参数的方法</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String method4(String name){
            System.out.println(</span>"有返回值，有参数的方法"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "哈哈" +<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有方法</span>
        <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method5(){
            System.out.println(</span>"私有方法"<span style="color: rgba(0, 0, 0, 1)">);
        }
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name=" + name + ", age=" + age + ", address=" + address+ "]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <h2>通过反射获取构造方法并使用</h2>
    <p>在反射机制中，把类中的成员（构造方法、成员方法、成员变量）都封装成了对应的类进行表示。其中，构造方法使用类Constructor表示。可通过Class类中提供的方法获取构造方法：</p>
    <p>返回一个构造方法：</p>
    <pre>public Constructor&lt;T&gt; getConstructor(Class&lt;?&gt;... parameterTypes) 获取public修饰, 指定参数类型所对应的构造方法<br>public Constructor&lt;T&gt; getDeclaredConstructor(Class&lt;?&gt;... parameterTypes) 获取指定参数类型所对应的构造方法(包含私有的)</pre>
    <p>返回多个构造方法</p>
    <pre>public Constructor&lt;?&gt;[] getConstructors() 获取所有的public 修饰的构造方法<br>public Constructor&lt;?&gt;[] getDeclaredConstructors() 获取所有的构造方法(包含私有的)</pre>
    <p>获取构造方法的代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ReflectConstructorDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取Class对象</span>
            Class c = Class.forName("typeinfo.Person");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">包名.类名
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Constructor[] cons = c.getConstructors();</span>
            Constructor[] cons =<span style="color: rgba(0, 0, 0, 1)"> c.getDeclaredConstructors();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Constructor con : cons) {
                System.out.println(con);
            }
            System.out.println(</span>"------------------------"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person()</span>
            Constructor con1 = c.getConstructor(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(con1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name)</span>
            Constructor con2 = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(con2);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private Person(String name, int age)</span>
            Constructor con3 = c.getDeclaredConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(con3);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name, int age, String address)</span>
            Constructor con4 = c.getDeclaredConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span>, String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(con4);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person(java.lang.String,int,java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private typeinfo.Person(java.lang.String,int)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person(java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        ------------------------
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person(java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private typeinfo.Person(java.lang.String,int)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public typeinfo.Person(java.lang.String,int,java.lang.String)</span></pre>
    </div>
    <h3>通过反射方式，获取构造方法，创建对象</h3>
    <p>步骤如下：</p>
    <p>1. 获取到Class对象</p>
    <p>2. 获取指定的构造方法</p>
    <p>3. 通过构造方法类Constructor中的方法&nbsp;<span class="cnblogs_code">newInstance</span>&nbsp;，创建对象</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    </span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ConstructorDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException, <br>                       InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1,获取到Class对象</span>
            Class c = Class.forName("typeinfo.Person");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">包名.类名
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2,获取指定的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Constructor con = c.getConstructor(null);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name, int age, String address)</span>
            Constructor con = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span>, String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3,通过构造方法类中Constructor的方法，创建对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Object obj = con.newInstance(null);</span>
            Object obj = con.newInstance("小明", 22, "哈尔滨"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示</span>
    <span style="color: rgba(0, 0, 0, 1)">        System.out.println(obj);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String, int, String的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Person [name=小明, age=22, address=哈尔滨]</span></pre>
    </div>
    <h3>通过反射方式，获取私有构造方法，创建对象</h3>
    <p>AccessibleObject 类是 Field、Method 和 Constructor 对象的父类。它提供了将反射的对象标记为在使用时取消默认 Java 语言访问控制检查的能力。</p>
    <p>对于公共成员、默认（打包）访问成员、受保护成员和私有成员，在分别使用 Field、Method 或 Constructor 对象来设置或获取字段、调用方法，或者创建和初始化类的新实例的时候，会执行访问检查。常用方法如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAccessible(<span style="color: rgba(0, 0, 255, 1)">boolean</span> flag) <span style="color: rgba(0, 0, 255, 1)">throws</span> SecurityException </pre>
    </div>
    <p>参数值为 true ，则指示反射的对象在使用时应该取消 Java 语言访问检查。参数值为 false&nbsp;则指示反射的对象应该实施 Java 语言访问检查。</p>
    <p>&nbsp;获取私有构造方法，步骤如下：</p>
    <p>1. 获取到Class对象</p>
    <p>2. 获取指定的构造方法</p>
    <p>3. 暴力访问, 通过setAccessible(boolean&nbsp;flag)方法</p>
    <p>4. 通过构造方法类Constructor中的&nbsp;<span class="cnblogs_code">newInstance</span>&nbsp;方法，创建对象</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ConstructorDemo2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException, <br>                                           InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1,获取到Class对象</span>
            Class c = Class.forName("typeinfo.Person");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">包名.类名
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2,获取指定的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private Person(String name, int age)</span>
            Constructor con = c.getDeclaredConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3,暴力反射</span>
            con.setAccessible(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取消 Java 语言访问检查
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4,通过构造方法类中的功能，创建对象</span>
            Object obj = con.newInstance("小明", 23<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(obj);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String，int的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Person [name=小明, age=23, address=null]</span></pre>
    </div>
    <h3>通过反射获取成员变量并使用</h3>
    <p>在反射机制中，把类中的成员变量使用类Field表示。可通过Class类中提供的方法获取成员变量：</p>
    <p>返回一个成员变量</p>
    <pre>public Field getField(String name) 获取指定的public修饰的变量<br>public Field getDeclaredField(String name) 获取指定的任意变量(包含私有)</pre>
    <p>返回多个成员变量</p>
    <pre>public Field[] getFields() 获取所有public修饰的变量<br>public Field[] getDeclaredFields() 获取所有的变量 (包含私有)</pre>
    <p>获取成员变量的代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeinfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Field;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> FieldDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchFieldException, SecurityException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取Class对象</span>
            Class c = Class.forName("typeinfo.Person"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取成员变量
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">多个变量
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Field[] fields = c.getFields();</span>
            Field[] fields =<span style="color: rgba(0, 0, 0, 1)"> c.getDeclaredFields();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Field field : fields) {
                System.out.println(field);
            }
            System.out.println(</span>"-----------------"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一个变量
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public int age;</span>
            Field ageField = c.getField("age"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(ageField);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private String address</span>
            Field addressField = c.getDeclaredField("address"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(addressField);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public java.lang.String typeinfo.Person.name
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public int typeinfo.Person.age
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private java.lang.String typeinfo.Person.address
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        -----------------
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public int typeinfo.Person.age
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private java.lang.String typeinfo.Person.address
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        Process finished with exit code 0</span></pre>
    </div>
    <h3>通过反射，创建对象，获取指定的成员变量，进行赋值与获取值操作</h3>
    <p>获取成员变量，步骤如下：</p>
    <p>1. 获取Class对象</p>
    <p>2. 获取构造方法</p>
    <p>3. 通过构造方法，创建对象</p>
    <p>4. 获取指定的成员变量（私有成员变量，通过setAccessible(boolean&nbsp;flag)方法暴力访问）</p>
    <p>5. 通过方法，给指定对象的指定成员变量赋值或者获取值&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> set(Object obj, Object value)</span>&nbsp;，在指定对象obj中，将此 Field 对象表示的成员变量设置为指定的新值。</p>
    <p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">public</span> Object get(Object obj) </span>&nbsp;返回指定对象obj中，此 Field 对象表示的成员变量的值。</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Field;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> FieldDemo2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
                IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1,获取Class对象</span>
            Class c = Class.forName("typeInfo.Person"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2，获取构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name)</span>
            Constructor con = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，通过构造方法，创建对象</span>
            Object obj = con.newInstance("小明"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4，获取指定的成员变量
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public String name;</span>
            Field nameField = c.getField("name"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public int age;</span>
            Field ageField = c.getField("age"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private String address;</span>
            Field addressField = c.getDeclaredField("address"<span style="color: rgba(0, 0, 0, 1)">);
            addressField.setAccessible(</span><span style="color: rgba(0, 0, 255, 1)">true</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取消 Java 语言访问检查
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5，通过方法，给指定对象的指定成员变量赋值或者获取值</span>
            System.out.println("name = "+<span style="color: rgba(0, 0, 0, 1)"> nameField.get(obj));
            System.out.println(</span>"age = "+<span style="color: rgba(0, 0, 0, 1)"> ageField.get(obj));
            System.out.println(</span>"address = "+<span style="color: rgba(0, 0, 0, 1)"> addressField.get(obj));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">赋值</span>
            ageField.set(obj, 23<span style="color: rgba(0, 0, 0, 1)">);
            addressField.set(obj, </span>"凯利广场"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"------------------------"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"name = "+<span style="color: rgba(0, 0, 0, 1)"> nameField.get(obj));
            System.out.println(</span>"age = "+<span style="color: rgba(0, 0, 0, 1)"> ageField.get(obj));
            System.out.println(</span>"address = "+<span style="color: rgba(0, 0, 0, 1)"> addressField.get(obj));
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">name = 小明
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">age = 0
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">address = null
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">------------------------
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">name = 小明
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">age = 23
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">address = 凯利广场</span></pre>
    </div>
    <h3><span style="line-height: 1.5">通过反射获取成员方法并使用</span></h3>
    <p>在反射机制中，把类中的成员方法使用类Method表示。可通过Class类中提供的方法获取成员方法：</p>
    <p>返回获取一个方法：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> Method getMethod(String name, Class&lt;?&gt;... parameterTypes)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取public 修饰的方法</span></pre>
    </div>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> Method getDeclaredMethod(String name, Class&lt;?&gt;... parameterTypes) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取任意的方法，包含私有的</span></pre>
    </div>
    <p>参数1: name 要查找的方法名称； 参数2： parameterTypes 该方法的参数类型</p>
    <p>返回获取多个方法：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Method[] getMethods() //获取本类与父类中所有public 修饰的方法
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Method[] getDeclaredMethods() //获取本类中所有的方法(包含私有的)</pre>
    </div>
    <p><span style="line-height: 1.5">获取成员方法的代码演示：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Method;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MethodDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取Class对象</span>
            Class c = Class.forName("typeInfo.Person"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取多个方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Method[] methods = c.getMethods();</span>
            Method[] methods =<span style="color: rgba(0, 0, 0, 1)"> c.getDeclaredMethods();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Method method : methods) {
                System.out.println(method);
            }
            System.out.println(</span>"-----------------------"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个方法：
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void method1()</span>
            Method method = c.getMethod("method1", <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(method);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public String method4(String name){</span>
            method = c.getMethod("method4", String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(method);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private void method5()</span>
            method = c.getDeclaredMethod("method5", <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(method);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public java.lang.String typeInfo.Person.toString()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void typeInfo.Person.method2(java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public int typeInfo.Person.method3()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public java.lang.String typeInfo.Person.method4(java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void typeInfo.Person.method1()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private void typeInfo.Person.method5()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">        -----------------------
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void typeInfo.Person.method1()
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public java.lang.String typeInfo.Person.method4(java.lang.String)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private void typeInfo.Person.method5()</span></pre>
    </div>
    <h3>通过反射，创建对象，调用指定的方法</h3>
    <p>获取成员方法，步骤如下：</p>
    <p>1. 获取Class对象</p>
    <p>2. 获取构造方法</p>
    <p>3. 通过构造方法，创建对象</p>
    <p>4. 获取指定的方法</p>
    <p>5. 执行找到的方法</p>
    <p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">public</span> Object invoke(Object obj, Object... args) </span>&nbsp;&nbsp;，执行指定对象obj中，当前Method对象所代表的方法，方法要传入的参数通过args指定。</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Method;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MethodDemo2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException,
                InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1， 获取Class对象</span>
            Class c = Class.forName("typeInfo.Person"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2,获取构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name, int age, String address){</span>
            Constructor con = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span>, String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，通过构造方法，创建对象</span>
            Object obj = con.newInstance("小明", 23, "哈尔滨"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4，获取指定的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void method1()  没有返回值没有参数的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Method m1 = c.getMethod("method1", null);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public String method4(String name)</span>
            Method m4 = c.getMethod("method4", String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5，执行找到的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">m1.invoke(obj, null);</span>
            Object result = m4.invoke(obj, "yyb"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"result = " +<span style="color: rgba(0, 0, 0, 1)"> result);
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String, int, String的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有返回值，有参数的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">result = 哈哈yyb</span></pre>
    </div>
    <h3>通过反射，创建对象，调用指定的private 方法</h3>
    <p>获取私有成员方法，步骤如下：</p>
    <p>1. 获取Class对象</p>
    <p>2. 获取构造方法</p>
    <p>3. 通过构造方法，创建对象</p>
    <p>4. 获取指定的方法</p>
    <p>5. 开启暴力访问</p>
    <p>6. 执行找到的方法</p>
    <p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">public</span> Object invoke(Object obj, Object... args)</span>&nbsp;，执行指定对象obj中，当前Method对象所代表的方法，方法要传入的参数通过args指定。</p>
    <p>代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Method;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MethodDemo3 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException,<br>                           IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1， 获取Class对象</span>
            Class c = Class.forName("typeInfo.Person"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2,获取构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public Person(String name, int age, String address){</span>
            Constructor con = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span>, String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，通过构造方法，创建对象</span>
            Object obj = con.newInstance("小明", 23, "哈尔滨"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4，获取指定的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private void method5(){</span>
            Method m5 = c.getDeclaredMethod("method5", <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5,开启暴力访问</span>
            m5.setAccessible(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6，执行找到的方法</span>
            m5.invoke(obj, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String, int, String的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有方法</span></pre>
    </div>
    <h3><span style="line-height: 1.5">&nbsp;</span><span style="line-height: 1.5">泛型擦除</span></h3>
    <p>将已存在的ArrayList&lt;Integer&gt;集合中添加一个字符串数据，如何实现呢？其实程序编译后产生的.class文件中是没有泛型约束的，这种现象我们称为泛型的擦除。那么，我们可以通过反射技术，来完成向有泛型约束的集合中，添加任意类型的元素。</p>
    <p>代码如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Method;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ReflectTest {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, NoSuchMethodException, SecurityException,
                IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
            ArrayList</span>&lt;Integer&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Integer&gt;<span style="color: rgba(0, 0, 0, 1)">();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加元素到集合</span>
            list.add(<span style="color: rgba(0, 0, 255, 1)">new</span> Integer(30<span style="color: rgba(0, 0, 0, 1)">));
            list.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Integer("12345"<span style="color: rgba(0, 0, 0, 1)">));
            list.add(</span>123<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">list.add("哈哈");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为有泛型类型的约束</span>
    <span style="color: rgba(0, 0, 0, 1)">        System.out.println(list);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过反射技术，实现添加任意类型的元素
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1, 获取字节码文件对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Class c = list.getClass();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Class c = ArrayList.class;</span>
            Class c = Class.forName("java.util.ArrayList"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2, 找到add()方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public boolean add(E e)</span>
            Method addMethod = c.getMethod("add", Object.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3，  执行add()方法</span>
            addMethod.invoke(list, "哈哈");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> list.add("哈哈");</span>
    <span style="color: rgba(0, 0, 0, 1)">        System.out.println(list);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[30, 12345, 123]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[30, 12345, 123, 哈哈]</span></pre>
    </div>
    <h3><span style="line-height: 1.5">反射配置文件</span></h3>
    <p>通过反射配置文件，运行配置文件中指定类的对应方法</p>
    <p>读取Peoperties.txt文件中的数据，通过反射技术，来完成Person对象的创建，Peoperties.txt（放在src目录下）文件内容如下：</p>
    <pre>className=typeInfo.Person<br>methodName=method5</pre>
    <p><span style="line-height: 1.5">读取配置文件，调用指定类中的对应方法</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> typeInfo;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileInputStream;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.FileNotFoundException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Constructor;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.InvocationTargetException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.reflect.Method;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Properties;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/4.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ReflectTest2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args)
                </span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> FileNotFoundException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException,
                InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过Properties集合从文件中读取数据</span>
            Properties prop = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取文件中的数据到集合中</span>
            prop.load(<span style="color: rgba(0, 0, 255, 1)">new</span> FileInputStream("src/properties.txt"<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取键所对应的值</span>
            String className = prop.getProperty("className"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(className);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1，获取Person.class 字节码文件对象</span>
            Class c =<span style="color: rgba(0, 0, 0, 1)"> Class.forName(className);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2，获取构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> public Person(String name, int age, String address)</span>
            Constructor con = c.getConstructor(String.<span style="color: rgba(0, 0, 255, 1)">class</span>, <span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span>, String.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3,创建对象</span>
            Object obj = con.newInstance("小明", 20, "中国"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(obj);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4，获取指定的方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> private void method5(){}</span>
            String methodName = prop.getProperty("methodName"<span style="color: rgba(0, 0, 0, 1)">);
            Method m5 </span>= c.getDeclaredMethod(methodName, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5,开启暴力访问</span>
            m5.setAccessible(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 6，执行找到的方法</span>
            m5.invoke(obj, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">typeInfo.Person
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带有String, int, String的构造方法
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Person [name=小明, age=20, address=中国]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有方法</span></pre>
    </div>
    </div>
    
</body>
</html>